package com.dragon.lib_mvi.base

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.PersistableBundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.jaeger.library.StatusBarUtil
import kotlin.math.ceil

/***
 * @ClassName BaseActivity
 * @Author Administrator
 * @description
 * @date 2025/2/24
 */
abstract class BaseActivity : AppCompatActivity() {
    private val handler: Handler = Handler(Looper.getMainLooper())

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        injectARouter()//注入路由,传参
        super.onCreate(savedInstanceState)
        //设置布局
        if (-1 == getLayoutId()) {
            setContentView(getLayoutView())
        } else {//有布局id
            setContentView(getLayoutId())
        }
        //设置状态栏
        StatusBarUtil.setColorNoTranslucent(this, generatStatusBarColor())
        //初始化环境
        initEnv()
        //初始化数据
        initData()
        //初始化view
        initView()
        //初始化事件监听
        initEvent()
    }

    protected open fun initEvent() {

    }

    protected open fun initView() {

    }

    protected open fun initData() {

    }

    protected open fun initEnv() {

    }

    /*
    * 设置状态栏颜色
    * */
    protected open fun generatStatusBarColor(): Int {
        return Color.TRANSPARENT
    }

    private fun injectARouter() {

    }

    //获取Content，view 配合ViewBinding使用
    abstract fun getLayoutView(): View

    protected open fun getLayoutId(layoutId: Int = -1): Int {
        return layoutId
    }

    /*
    * 显示提示消息
    * */
    fun showMsg(msg: String) {
        if (Looper.getMainLooper().thread == Thread.currentThread()) {//判读当前线程是否是主线程
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
        } else {
            handler.post {
                Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
            }
        }
    }

    /*
    * 组件内跳转到指定页面
    * */
    fun routeActivity(target: Class<*>) {
        startActivity(Intent(this, target))
    }

    /*
    * 组件内跳转到指定页面并传递参数
    * */
    fun routeActivity(target: Class<*>, bundle: Bundle) {
        val intent = Intent(this, target)
        intent.putExtra("params", bundle)
        startActivity(intent)
    }

    fun addFragment(fragment: Fragment, tag: String) {
        supportFragmentManager.beginTransaction().add(fragment, tag).commitAllowingStateLoss()
    }

    fun replaceFragment(containerId: Int, fragment: Fragment) {
        supportFragmentManager.beginTransaction().replace(containerId, fragment)
            .commitAllowingStateLoss()
    }

    fun showFragment(tag: String) {
        supportFragmentManager.findFragmentByTag(tag)?.let {
            supportFragmentManager.beginTransaction().show(it).commitAllowingStateLoss()
        }
    }

    //切换Fragment
    fun switchFragment(fl: Int, fragment: Fragment?, tag: Fragment) {
        if (!tag.isAdded) {
            supportFragmentManager.beginTransaction().hide(fragment!!).add(fl, tag).commit()
        } else {
            supportFragmentManager.beginTransaction().hide(fragment!!).show(tag).commit()
        }
    }

    fun closeThis() {
        finish()
    }

    /*
    * 获取状态栏高度
    * */
    private fun getStatusBarHeight(): Int {
        val identifier = resources.getIdentifier("status_bar_height", "dimen", "android")
        return if (identifier > 0) {
            resources.getDimensionPixelSize(identifier)
        } else {
            val density = resources.displayMetrics.density
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                ceil(24 * density).toInt()
            } else {
                ceil(25 * density).toInt()
            }
        }
    }
}